package com.demo.flight.service.impl;

import com.demo.flight.entity.Flight;
import com.demo.flight.mapper.FlightMapper;
import com.demo.flight.service.FlightService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author ssy
 * @create 2022-03-23 17:58
 */
@Service
public class FlightServiceImpl implements FlightService {
    
    @Autowired
    private FlightMapper flightMapper;
    
    @Override
    public Flight getOneById(Integer flightId) {
        return flightMapper.selectOneById(flightId);
    }
    
    @Override
    public List<Flight> getAll() {
        return flightMapper.selectAll();
    }
    
    @Override
    public boolean add(Flight flight) {
//        Flight tmp = new Flight();
//        tmp.setDepartPlace(flight.getDepartPlace());
//        tmp.setDepartTime(flight.getDepartTime());
//        tmp.setArrivePlace(flight.getArrivePlace());
//        tmp.setArriveTime(flight.getArriveTime());
//        tmp.setCost(flight.getCost());
        if (flightMapper.countByCondition(flight) == 0) {
            flightMapper.insert(flight);
            return true;
        }
        return false;
    }
    
    @Override
    public boolean delete(Integer flightId) {
        if (flightMapper.countById(flightId) == 1) {
            flightMapper.deleteById(flightId);
            return true;
        }
        return false;
    }
    
    @Override
    public boolean update(Flight flight) {
        if (flightMapper.countById(flight.getFlightId()) == 1) {
            flightMapper.update(flight);
            return true;
        }
        return false;
    }
}
